#pragma once
#include <QObject>
#include <QString>
#include <QMutex>
#include <QFile>
#include <QTimer>
#include <QDate>
#include <QSharedPointer>

enum {
	LOG_INFO,
	LOG_WARNING,
	LOG_ERROR,
	LOG_FATAL,
};

class LogObject 
{
public:
	QString time;
	QString module;
	int level;
	QString message;
};
typedef QSharedPointer<LogObject> LogObjectPtr;

class LogManager : public QObject
{
	Q_OBJECT

public:		
	static LogManager *instance();		

	void init();
	void deinit();
	void log(const char * module, int level, const char *format, ...);

signals:
	void sigLog(LogObjectPtr log);

protected slots:
	void onTimeOut();

protected:
	LogManager();
	~LogManager();
	void createFile();

	QMutex mutex;
	QTimer timer;
	QFile generalFile;
	QFile luaFile;
	QDate curDate;
	bool m_init;
};

#define LogInfo(...)    LogManager::instance()->log("general",LOG_INFO,__VA_ARGS__)
#define LogWarning(...) LogManager::instance()->log("general",LOG_WARNING,__VA_ARGS__)
#define LogError(...)   LogManager::instance()->log("general",LOG_ERROR,__VA_ARGS__)
#define LogFatal(...)   LogManager::instance()->log("general",LOG_FATAL,__VA_ARGS__)